package com.southwind.controller;

import com.southwind.entity.OperateLog;
import com.southwind.service.OperateLogService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/operateLog")
public class OperateLogController {

    private final OperateLogService operateLogService;

    public OperateLogController(OperateLogService operateLogService) {
        this.operateLogService = operateLogService;
    }

    /**
     * 插入操作日志
     * @param log 前端传入日志对象
     * @return 状态信息
     */
    @PostMapping("/insert")
    public Map<String, Object> insert(@RequestBody OperateLog log) {
        log.setOperateTime(LocalDateTime.now()); // 可选：自动设置当前时间
        operateLogService.insert(log);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "日志记录成功");
        return result;
    }

    @GetMapping("/list")
    public Map<String, Object> listAll() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<OperateLog> logs = operateLogService.listAll();
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", logs);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询异常: " + e.getMessage());
        }
        return result;
    }


}
